import yaml
import os

class YamlReader:
    def __init__(self):
        try:
            with open(os.path.dirname(os.path.dirname(__file__))+'/DataCenter/ElementsCenter.yml','r',encoding='utf-8') as file:
                self.data = yaml.load(file,Loader=yaml.FullLoader)

        except:
            raise FileExistsError("文件没有找到")
    def get_url(self,page,url_name):
        url = self.data[page][url_name]
        if url['url']:
            return url['url']
        """
        读取url
        :param page:
        :param url_name:
        :return:
        """
        # return self.data[page][url_name]
    def get_element(self,page,element_name):
        element = self.data[page][element_name]
        if element['locater_by']:
            if element['locater_by'].lower() == 'xpath':
                by = 'xpath'
            elif element['locater_by'].lower() == 'id':
                by = 'id'
            elif element['locater_by'].lower() == 'link_text':
                by = 'link_text'
            elif element['locater_by'].lower() == 'partial_link_text':
                by = 'partial_link_text'
            elif element['locater_by'].lower() == 'tag_name':
                by = 'tag_name'
            elif element['locater_by'].lower() == 'class_name':
                by = 'class_name'
            elif element['locater_by'].lower() == 'css':
                by = 'css selector'
            else:
                raise Exception(f'定位方式不存在{element["locater_by"]}')
        else:
            raise Exception(f'定位方式不应该为空')

        value = element["value"]

        return [by,value]

if __name__ == '__main__':
    # print(YamlReader().get_element("LoginPage","username"))
    print(YamlReader().get_url("PLS_Home","url"))
    # print(YamlReader().get_element("LoginPage1","username"))
    print(YamlReader().get_element("PLS_Home","search"))
    # print(YamlReader().get_element("LoginPage1","username"))